package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author liuyaning
 * @version 1.0
 * @date 2021/7/29 15:04
 */
@Service
@Transactional
@Slf4j
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService{
    @Override
    public ResponseResult list(ChannelDto channelDto) {
        //1 判断参数是否为空
        if (channelDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //2 初始化分页数据
        channelDto.checkParam();

        //3判断参数中的名称是否为空，决定是否拼接参数
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(channelDto.getName())){
            queryWrapper.like(AdChannel::getName,channelDto.getName());
        }

        //分页
        IPage<AdChannel> page = new Page<>(channelDto.getPage(),channelDto.getSize());
        page(page, queryWrapper);

        //响应数据
        ResponseResult responseResult = new PageResponseResult(channelDto.getPage(),channelDto.getSize(), (int) page.getTotal());
        //获取到列表信息
        responseResult.setData(page.getRecords());
        return responseResult;

    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //判断参数是否为空
        if(adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //设置其他字段
        adChannel.setIsDefault(false);
        adChannel.setCreatedTime(new Date());
        //新增频道
        save(adChannel);
        //想用数据
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        //判断参数
        if(adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断该频道是否存在
        AdChannel adChannelById = getById(adChannel.getId());
        if (adChannelById == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //根据id更新
        updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult del(Integer id) {
        //判断参数
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断是否存再
        AdChannel channel = getById(id);
        if(channel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断该频道是否启用
        if (channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIC_ERROR);
        }


        //执行删除
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
